package org.nado.member;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.SessionAttributes;

@WebServlet(name="passwordmaker", urlPatterns = {"/member/resources/password", "/member/get/resources/password", "/member/chkid/resources/password"})
@SessionAttributes("randomStr")
public class PasswordMaker extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public PasswordMaker() {
        super();
    }

	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("image/jpeg");
		
		BufferedImage img = new BufferedImage(200, 80, BufferedImage.TYPE_INT_RGB);
		OutputStream out = response.getOutputStream();

		Font font = new Font("batang", Font.BOLD, 30);
		
		Graphics g = img.getGraphics();
		g.setFont(font);
		g.setColor(Color.PINK);
		g.fillRect(0, 0, 300, 200);
		g.setColor(Color.BLACK);
		
		int rnd = (int)((Math.random()*100)+1);
		char rnd1 = (char)((Math.random()*25)+65);
		char rnd2 = (char)((Math.random()*25)+65);
		char rnd3 = (char)((Math.random()*25)+65);
		int rnd4 = (int)((Math.random()*100)+1);
		char rnd5 = (char)((Math.random()*25)+65);
		
		String str = "" + rnd + rnd1 + rnd2 + rnd3 + rnd4 + rnd5;
		
		g.drawString(str, 10, 50);
				
		ImageIO.write(img, "JPG", out);
		
		HttpSession session = request.getSession(true);
		session.setAttribute("randomStr", str);
	
		
	}
}
